package day09;

import java.util.Random;

public abstract class People {
}

class TestPeople {
    public static void main(String[] args) {
        Man husband = new Man();
        Woman wife = new Woman();
        husband.setWife(wife);
        wife.setHusband(husband);

        People baby = wife.getBaby();
        if (baby instanceof Man) {
            //转换成Man类型 类型转换
            /*
            如果父类类型变量值转换成子类类型：向下转型 语法格式类似于强制转换

             */
            ((Man) baby).playLol();
        } else if (baby instanceof Woman) {
            ((Woman) baby).shopping();
        } else
            System.out.println("没有老公生毛线");
    }
}

class Man extends People {
    private Woman wife;

    public Woman getWife() {
        return wife;
    }

    public void setWife(Woman wife) {
        this.wife = wife;
    }

    public void playLol() {
        System.out.println("打lol");
    }
}

class Woman extends People {
    private Man husband;

    public Man getHusband() {
        return husband;
    }

    public void setHusband(Man husband) {
        this.husband = husband;
    }

    public void shopping() {
        System.out.println("老婆在购物");
    }

    public People getBaby() {
        if (husband != null) {
            Random random = new Random();
            int a = random.nextInt(2);
            if (a == 0) {
                return new Man();
            } else {
                return new Woman();
            }
        }
        return null;
    }
}
